<?php

/**
 * Subject filter form base class.
 *
 * @package    wiki
 * @subpackage filter
 * @author     Elmaho
 * @version    SVN: $Id: sfDoctrineFormFilterGeneratedTemplate.php 29570 2010-05-21 14:49:47Z Kris.Wallsmith $
 */
abstract class BaseSubjectFormFilter extends BaseFormFilterDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'title_sjt'        => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'summary_sjt'      => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'article_sjt'      => new sfWidgetFormFilterInput(),
      'link_sjt'         => new sfWidgetFormFilterInput(),
      'publication_year' => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'read_times'       => new sfWidgetFormFilterInput(),
      'domain_id'        => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Domain'), 'add_empty' => true)),
      'created_at'       => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => false)),
      'updated_at'       => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => false)),
      'slug'             => new sfWidgetFormFilterInput(),
      'user_list'        => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Person')),
      'author_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Person')),
      'event_list'       => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Event')),
    ));

    $this->setValidators(array(
      'title_sjt'        => new sfValidatorPass(array('required' => false)),
      'summary_sjt'      => new sfValidatorPass(array('required' => false)),
      'article_sjt'      => new sfValidatorPass(array('required' => false)),
      'link_sjt'         => new sfValidatorPass(array('required' => false)),
      'publication_year' => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'read_times'       => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'domain_id'        => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('Domain'), 'column' => 'id')),
      'created_at'       => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59')))),
      'updated_at'       => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59')))),
      'slug'             => new sfValidatorPass(array('required' => false)),
      'user_list'        => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Person', 'required' => false)),
      'author_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Person', 'required' => false)),
      'event_list'       => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Event', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('subject_filters[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function addUserListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.Comment Comment')
      ->andWhereIn('Comment.person_id', $values)
    ;
  }

  public function addAuthorListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.AuthorSubject AuthorSubject')
      ->andWhereIn('AuthorSubject.person_id', $values)
    ;
  }

  public function addEventListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.EventSubject EventSubject')
      ->andWhereIn('EventSubject.event_id', $values)
    ;
  }

  public function getModelName()
  {
    return 'Subject';
  }

  public function getFields()
  {
    return array(
      'id'               => 'Number',
      'title_sjt'        => 'Text',
      'summary_sjt'      => 'Text',
      'article_sjt'      => 'Text',
      'link_sjt'         => 'Text',
      'publication_year' => 'Number',
      'read_times'       => 'Number',
      'domain_id'        => 'ForeignKey',
      'created_at'       => 'Date',
      'updated_at'       => 'Date',
      'slug'             => 'Text',
      'user_list'        => 'ManyKey',
      'author_list'      => 'ManyKey',
      'event_list'       => 'ManyKey',
    );
  }
}
